<?php
/**
 * @author Sebastian Staudt
 * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @package Steam Condenser (PHP)
 * @subpackage SteamSocket
 * @version $Id: SteamSocket.php 102 2008-08-09 17:41:46Z koraktor $
 */

require_once "ByteBuffer.php";
require_once "DatagramChannel.php";
require_once "InetAddress.php";
require_once "exceptions/TimeoutException.php";
require_once "steam/packets/SteamPacket.php";

/**
 * @package Steam Condenser (PHP)
 * @subpackage SteamSocket
 */
abstract class SteamSocket
{
	/**
	 * @var ByteBuffer
	 */
	protected $buffer;
	
	/**
	 * @var DatagramChannel
	 */
	protected $channel;
	
	public function __construct(InetAddress $ipAddress, $portNumber = 27015)
	{
    $this->channel = DatagramChannel::open();
    $this->channel->connect($ipAddress, $portNumber);
    $this->channel->configureBlocking(false);	
	}
	
	public function __destruct()
	{
		//$this->channel->close();
	}
	
	protected function createPacket()
	{
    return SteamPacket::createPacket($this->buffer->get());
  }
	
  /**
   * Abstract getReply() method
   * @return SteamPacket
   */
  abstract public function getReply();
  
  /**
   * @return int
   */
  public function receivePacket($bufferLength = 0)
  {
  	if($bufferLength == 0)
  	{
  		if(!$this->channel->socket()->select())
  		{
  			return 0;
  		}
  		$this->buffer->clear();
  	}
  	else
  	{
  		if(!$this->channel->socket()->select(5))
  		{
  			throw new TimeoutException();
  		}
  		$this->buffer = ByteBuffer::allocate($bufferLength);
  	}
  	
  	$this->channel->read($this->buffer);
  	$bytesRead = $this->buffer->position();
  	$this->buffer->rewind();
  	$this->buffer->limit($bytesRead);
  	
  	return $bytesRead;
  }
	
	/**
	 *
	 */
	public function send(SteamPacket $dataPacket)
	{
		debug("Sending packet of type \"" . get_class($dataPacket) . "\"...");
		
		$this->buffer = ByteBuffer::wrap($dataPacket->__toString());
		$this->channel->write($this->buffer);
	}
}
?>
